package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemEnterpriseSingleVideoBinding;
import com.sgb.kjwl.databinding.ItemPatentBinding;
import com.sgb.kjwl.model.entity.enterprise.PatentEntity;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.adapter.transport.GoodsInfoVideoAdapter;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class HomePatentAdapter extends BaseMultiItemQuickAdapter<PatentEntity, BaseViewHolder> {
    Context context;

    public HomePatentAdapter(Context context, List<PatentEntity> datas) {
        super(datas);
        this.context = context;
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.item_patent);
        //视频类别
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.item_enterprise_video_list);
        //单个视频类别
        addItemType(SingleItemConstant.TYPE_THREE, R.layout.item_enterprise_single_video);
    }

    @Override
    protected void convert(BaseViewHolder helper, PatentEntity item) {
        switch (item.getItemType()) {
            case SingleItemConstant.TYPE_ONE:
                setPatent(helper, item);
                break;
            case SingleItemConstant.TYPE_TWO:
                initVideoList(helper, item);
                break;
            case SingleItemConstant.TYPE_THREE:
                initSingleVideo(helper, item);
                break;
        }

    }

    private void setPatent(BaseViewHolder holder, PatentEntity patentEntity) {
        ItemPatentBinding binding = DataBindingUtil.bind(holder.itemView);
        binding.setViewModel(patentEntity);
        switch (patentEntity.getPublishType()) {
            case 0://个人
                binding.tvItemIntelPropPublish.setText("个人");
                binding.tvItemIntelPropPublish.setBackgroundResource(R.drawable.shape_radius_1_solid_bule);
                break;
            case 1://企业
                binding.tvItemIntelPropPublish.setText("企业");
                binding.tvItemIntelPropPublish.setBackgroundResource(R.drawable.shape_radius_1_solid_bule);
                break;
            case 2://团队
                binding.tvItemIntelPropPublish.setText("团队");
                binding.tvItemIntelPropPublish.setBackgroundResource(R.drawable.ractangle_radius_1_solid_yellow);
                break;
            default:
                binding.tvItemIntelPropPublish.setVisibility(View.GONE);
                break;
        }
        if (patentEntity.getLabel() != null && patentEntity.getLabel().contains("1")) {
            holder.setVisible(R.id.tv_item_intel_prop_hot, true);
        } else {
            holder.setVisible(R.id.tv_item_intel_prop_hot, false);
        }
        if (patentEntity.getLabel() != null && patentEntity.getLabel().contains("2")) {
            holder.setVisible(R.id.tv_item_intel_prop_urgent_sale, true);
        } else {
            holder.setVisible(R.id.tv_item_intel_prop_urgent_sale, false);
        }

        binding.btnChat.setOnClickListener((View.OnClickListener) view -> {
            if (DialogHelper.checkLogin()) {
                DialogHelper.showDialog(context, "确认呼叫", patentEntity.getContacts() + "：" + patentEntity.getContactNumber(), "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        ActivityJump.jumpToPhone(context, patentEntity.getContactNumber());
                    }
                });
            } else {
                DialogHelper.showLoginDialogNew(context, EventConstant.EVENT_INTELL_PROP_FRG_JUMP, "温馨提示", "登录后可拨打电话,去登录", "登录");
            }

        });

        if (patentEntity.getImg() != null && !patentEntity.getImg().equals("")) {
            Glide.with(context).load(patentEntity.getImg()).apply(RequestOptions.bitmapTransform(new RoundedCorners(2))).into(binding.logoView);
        } else {
            switch (patentEntity.getCategoryName()) {
                case "发明专利":
                    Glide.with(context).load(R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish).into(binding.logoView);
                    break;
                case "实用新型":
                    Glide.with(context).load(R.mipmap.shiyongxinxing_patent_publish).into(binding.logoView);
                    break;
                case "外观设计":
                    Glide.with(context).load(R.mipmap.waiguansheji_patent_publish).into(binding.logoView);
                    break;
            }
        }
    }

    /**
     * 设置视频信息
     */
    private void initVideoList(BaseViewHolder helper, PatentEntity entity) {
        List<VideoBean> videoBeanList = new ArrayList<>();
        videoBeanList.addAll(entity.getVideos());
        RecyclerView recyclerView = helper.getView(R.id.list_data);
        if (entity.getVideos() == null || entity.getVideos().size() == 0) {
            recyclerView.setVisibility(View.GONE);
            return;
        }
        recyclerView.setVisibility(View.VISIBLE);
        if (recyclerView.getAdapter() == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            GoodsInfoVideoAdapter adapter = new GoodsInfoVideoAdapter(mContext, videoBeanList);
            adapter.setOnItemClick(new AdapterOnItemClick<VideoBean>() {
                @Override
                public void onItemClick(VideoBean homeHostVideoEntity, int position) {
                    Intent intent = new Intent(mContext, PlayListActivity.class);
                    intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) adapter.getDatas());
                    intent.putExtra(PlayListActivity.PSO, position);
                    intent.putExtra(PlayListActivity.VIDEOTYPE, "goods_and_car_video");
                    mContext.startActivity(intent);
                }
            });
            recyclerView.setAdapter(adapter);
        } else {
            ((GoodsInfoVideoAdapter) recyclerView.getAdapter()).setDatas(videoBeanList);
        }
    }

    /**
     * 设置单个视频信息
     */
    private void initSingleVideo(BaseViewHolder helper, PatentEntity entity) {
        //设置dataBinding数据
        ItemEnterpriseSingleVideoBinding binding = DataBindingUtil.bind(helper.itemView);
        if (binding == null) return;
        if (entity.getVideos() != null && !entity.getVideos().isEmpty()) {
            binding.setVariable(BR.viewModel, entity.getVideos().get(0));
            binding.executePendingBindings();
        }
    }
}
